/*
	Copyright (C) 2004-2005 Cory Nelson

	This software is provided 'as-is', without any express or implied
	warranty.  In no event will the authors be held liable for any damages
	arising from the use of this software.

	Permission is granted to anyone to use this software for any purpose,
	including commercial applications, and to alter it and redistribute it
	freely, subject to the following restrictions:

	1. The origin of this software must not be misrepresented; you must not
		claim that you wrote the original software. If you use this software
		in a product, an acknowledgment in the product documentation would be
		appreciated but is not required.
	2. Altered source versions must be plainly marked as such, and must not be
		misrepresented as being the original software.
	3. This notice may not be removed or altered from any source distribution.
	
	CVS Info :
		$Author: phrostbyte $
		$Date: 2005/06/16 20:46:40 $
		$Revision: 1.1 $
*/
#include "sqlite3\\sqlite3Ex.h"
#include "sqlite3x.hpp"

extern PFN_Sqlite3_column_count		g_pfnsqlite3_column_count;									
extern PFN_Sqlite3_step           g_pfnsqlite3_step;
extern PFN_Sqlite3_data_count     g_pfnsqlite3_data_count;
extern PFN_Sqlite3_reset          g_pfnsqlite3_reset;
extern PFN_Sqlite3_column_int     g_pfnsqlite3_column_int;
extern PFN_Sqlite3_column_int64   g_pfnsqlite3_column_int64;
extern PFN_Sqlite3_column_double  g_pfnsqlite3_column_double;
extern PFN_Sqlite3_column_text    g_pfnsqlite3_column_text;
extern PFN_Sqlite3_column_bytes   g_pfnsqlite3_column_bytes;
extern PFN_Sqlite3_column_text16  g_pfnsqlite3_column_text16;
extern PFN_Sqlite3_column_bytes16 g_pfnsqlite3_column_bytes16;
extern PFN_Sqlite3_column_blob    g_pfnsqlite3_column_blob;
extern PFN_Sqlite3_column_name    g_pfnsqlite3_column_name;
extern PFN_Sqlite3_column_name16  g_pfnsqlite3_column_name16;
extern PFN_Sqlite3_errmsg         g_pfnsqlite3_errmsg;

namespace sqlite3x {

sqlite3_reader::sqlite3_reader() : m_pCmd(NULL) 
{
}

sqlite3_reader::sqlite3_reader(const sqlite3_reader &copy) : m_pCmd( copy.m_pCmd )
{
	if ( m_pCmd )
		++ m_pCmd->refs;
}

sqlite3_reader::sqlite3_reader(sqlite3_command *cmd) : m_pCmd(cmd)
{
	++ m_pCmd->refs;
}

sqlite3_reader::~sqlite3_reader()
{
	close();
}

sqlite3_reader& sqlite3_reader::operator=(const sqlite3_reader &copy)
{
	close();

	m_pCmd = copy.m_pCmd;
	if ( m_pCmd )
		++m_pCmd->refs;

	return *this;
}

bool sqlite3_reader::read()
{
	if ( !m_pCmd )
	{
		throw database_error("reader is closed");
	}
	int iRet = g_pfnsqlite3_step( m_pCmd->stmt ) ;
	switch ( iRet )
	{
	case SQLITE_ROW:
		{
			return true;
		}
	case SQLITE_DONE:
		{
			return false;
		}
	default:
		{
			throw database_error( m_pCmd->con );
		}
	}
}

long sqlite3_reader::GetRecrodCount()
{
	if( !m_pCmd ) throw database_error("reader is closed");

	return g_pfnsqlite3_column_count( m_pCmd->stmt );
}


void sqlite3_reader::reset() {
	if( !m_pCmd ) throw database_error("reader is closed");

	if(g_pfnsqlite3_reset(m_pCmd->stmt)!=SQLITE_OK)
		throw database_error( m_pCmd->con );
}

void sqlite3_reader::close()
{
	if ( m_pCmd )
	{
		if ( --m_pCmd->refs == 0 )
			g_pfnsqlite3_reset( m_pCmd->stmt );
		m_pCmd = NULL;
	}
}

int sqlite3_reader::getint(int index)
{
	if( !m_pCmd )
		throw database_error("reader is closed");

	if( (index) > (m_pCmd->m_nArgc-1) )
		throw out_of_range("index out of range");

	return g_pfnsqlite3_column_int( m_pCmd->stmt, index );
}

long long sqlite3_reader::getint64(int index) {
	if( !m_pCmd )
		throw database_error("reader is closed");

	if( (index) > (m_pCmd->m_nArgc-1) )
		throw out_of_range("index out of range");

	return g_pfnsqlite3_column_int64( m_pCmd->stmt, index );
}

double sqlite3_reader::getdouble(int index) {
	if( !m_pCmd )
		throw database_error("reader is closed");

	if( (index) > (m_pCmd->m_nArgc-1) )
		throw out_of_range("index out of range");

	return g_pfnsqlite3_column_double( m_pCmd->stmt, index );
}

string sqlite3_reader::getstring(int index)
{
	if( !m_pCmd )
		throw database_error("reader is closed");

	if( (index) > (m_pCmd->m_nArgc-1) )
		throw out_of_range("index out of range");

	return string( (const char*)g_pfnsqlite3_column_text( m_pCmd->stmt, index ), g_pfnsqlite3_column_bytes( m_pCmd->stmt, index ) );
}

wstring sqlite3_reader::getstring16(int index)
{
	if( !m_pCmd )
		throw database_error("reader is closed");

	if( (index) > (m_pCmd->m_nArgc-1) )
		throw out_of_range("index out of range");

	return wstring( (const wchar_t*)g_pfnsqlite3_column_text16( m_pCmd->stmt, index ), g_pfnsqlite3_column_bytes16( m_pCmd->stmt, index )/2 );
}

string sqlite3_reader::getblob(int index) {
	if( !m_pCmd )
		throw database_error("reader is closed");

	if( (index) > (m_pCmd->m_nArgc-1) )
		throw out_of_range("index out of range");

	return string( (const char*)g_pfnsqlite3_column_blob( m_pCmd->stmt, index ), g_pfnsqlite3_column_bytes( m_pCmd->stmt, index ) );
}

string sqlite3_reader::getcolname(int index) {
	if( !m_pCmd )
		throw database_error("reader is closed");

	if( (index) > (m_pCmd->m_nArgc-1) )
		throw out_of_range("index out of range");

	return g_pfnsqlite3_column_name( m_pCmd->stmt, index );
}

wstring sqlite3_reader::getcolname16(int index) {
	if( !m_pCmd )
		throw database_error("reader is closed");

	if( (index) > (m_pCmd->m_nArgc-1) )
		throw out_of_range("index out of range");

	return (const wchar_t*)g_pfnsqlite3_column_name16( m_pCmd->stmt, index );
}

}
